import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App.vue'
import router from './router/index'
import store from './store'
import './api/mock'
import Cookie from 'js-cookie'

Vue.config.productionTip = false
Vue.use(ElementUI)//全局注入
//添加全局前置导航守卫
router.beforeEach((to,from,next) => {
  //token是否存在
 const token = Cookie.get('token')
 //如果token不存在说明用户没登陆
 if(!token && to.name !== 'login'){
  next({ name:'login' })
 }
 else if(token&& to.name === 'login') {
  next({name:'home'})
 }
 else {
  next();
 }
})
new Vue({
  render: h => h(App),
router,
store,

}).$mount('#app')
